A Simple Custom Handler for a Lister

back to section start!
  To my mind the difference between a Custom Handler and a Module could be
given as:

  A Custom Handler allows you to 'capture events' from a lister or an
AppIcon and act upon them.

  A Module is a way of adding things to the system.  As Helmut has said in
his 'C' tutorial: 'A module is just a library of Opus routines'.

  You can have a module that includes a custom handler, Leo Davidson's
PrintAppIcon script is a demonstration of this, but each is a separate
entity within the script.

  An event happens every time you select something from a toolbar, popup
menu, do a Drag'n'Drop, and a few other things.

  Custom handlers usually open their own lister or AppIcon, and then they
wait for you to do something, or they can be attached to an already open
lister.

  Edmund Vermeulen's ArcDir script, (available from Aminet), is an excellent
example of what can be done using custom handlers.

  Below is a really simple example of a custom handler for a lister, in fact
it doesn't do anything useful at all, you can just select files and use Opus
commands, nothing will happen.
  Every action you perform on the lister will be reported in a console
window.

 1/*
 2$VER: SLCH.dopus5 1.0 (26.9.98)
 3Simple Lister custom handler
 4*/
 5signal on error
 6signal on syntax
 7signal on halt
 8signal on break_c
 9
10address DOPUS.1
11options results
12options failat 11
13
14dopus front
15
16if ~show('l','rexxsupport.library') then
17  call addlib('rexxsupport.library',,-30)
18
19call open('window','CON:0/12/640/240/SLCH.dopus5')
20
21lister new
22handle = result
23lister read handle 'SYS:' force
24lister wait handle
25call openport('SLCH-handler')
26lister set handle handler 'SLCH-handler' subdrop quotes
27
28/* We are going to trap everything */
29dopus addtrap '*' 'SLCH-handler'
30
31do while event ~= 'inactive'
32  if waitpkt('SLCH-handler') then do
33    packet = getpkt('SLCH-handler')
34    if packet ~= '00000000'x then do
35      event = getarg(packet,0)
36      handle = getarg(packet,1)
37      name = getarg(packet,2)
38      user = getarg(packet,3)
39      pathstr = getarg(packet,4)
40      arguments = getarg(packet,5)
41      qualifier = getarg(packet,6)
42      deststr = getarg(packet,7)
43
44      call writeln('window','--------------------')
45      call writeln('window','Arg0 (event)       =' event)
46      call writeln('window','Arg1 (handle)      =' handle)
47      call writeln('window','Arg2 (name)        =' name)
48      call writeln('window','Arg3 (user)        =' user)
49      call writeln('window','Arg4 (path)        =' pathstr)
50      call writeln('window','Arg5 (arguments)   =' arguments)
51      call writeln('window','Arg6 (qualifier)   =' qualifier)
52      call writeln('window','Arg7 (destination) =' deststr)
53      end
54    call reply(packet,0)
55    end
56end
57
58/* remove all traps for my handler */
59error:
60syntax:
61halt:
62break_c:
63dopus remtrap '*' 'SLCH-handler'
64call closeport('SLCH-handler')
65if rc = 0 then call delay(100)
66call close('window')
67if rc ~= 0 then do
68  text = 'Error: 'rc', 'errortext(rc)' in line 'sigl'.'
69  dopus request '"'text'" OK'
70  end
71exit

Line:
 1 - 4  The obligatory ARexx comment.
 5 - 8  If anything goes wrong with the script we'll jump to the error
        handling routine down at line 59.
10 - 12 Address the Opus ARexx port, enable ARexx results and set the failat
        limit at 11.
14      Move the Opus screen/window to the front.
16 - 17 If the rexxsupport.library isn't loaded then we load it, we need the
        openport, closeport and delay commands it contains.
19      Open our output window.
21 - 24 Open the lister, assign handle to it's handle, read in the SYS:
        directory and wait until it's finished.
25      Call the openport command to open a system message port called
        SLCH-handler.

  NOTE: Message port names are case-sensitive, you will now need to refer to
        this message port exactly as typed.

26      Set the handler to the lister using the  lister set handler  command.
29      We are going to trap every event so we use the '*' to signify this.
        Otherwise we could trap specific events by specifying them, for
        example:  dopus addtrap 'Copy' 'SLCH-handler'
31      We enter a DO loop and will only exit when the event equals
        'inactive', which means the lister has been closed.
32      This is where the script normally 'parks' while waiting for something
        to happen, as soon as it receives a message packet on our port this
        statement will become true and the script will continue.
33      We get the message packet in variable packet.
34 - 52 If it isn't a null packet we get the various arguments from it
        assigning them to variables.  We then write them to our output
        console.

54      We reply to the message packet,  THIS IS IMPORTANT YOU MUST DO IT!!

59 - 62 If we happen to have received some kind of error then we would have
        jumped to one of these labels, otherwise when the lister is closed
        we'll exit the DO loop and progress through here anyway.
63      We remove all the traps that we placed upon this handler.
64      Call the closeport command to close our message port.
65      If we exit normally then we'll delay for two seconds.
66      Close our output console.
67 - 70 If we didn't exit normally, (error condition), then we format the
        error information into a text string and display it in a requester.
71      Exit.

DOpus PLUS - giving you that bit extra...